import bh_toast from "./bh_toast";
import { privacyChannelKS } from "./privacyChannelKS";
import { privacyChannelKSinner } from "./privacyChannelKSinner";
import { pwrChannelMg } from "./privacyChannelMg";
import { privacyChannelOppo } from "./privacyChannelOppo";
import { privacyChannelOppo1 } from "./privacyChannelOppo1";
import { privacyChannelVivo } from "./privacyChannelVivo";
import { privacyChannelVivo1 } from "./privacyChannelVivo1";
import { privacyChannelWeb } from "./privacyChannelWeb";
import { privacyChannelXM } from "./privacyChannelXM";
import privacyWin from "./privacyWin";

/**
 * 分渠道打包处理,出包时配置其中的channel值为渠道对象
 *
 */
export class privacyChannel {


    /** 当前渠道,出包在此配置渠道, 仅当remoteVerKey为debug时有效否则会变成web  */
    static channel: string = 'vivo1';

    static channelMap: Map<String, privacyChannelWeb> = new Map([

        ['oppo', new privacyChannelOppo()],
        ['oppo1', new privacyChannelOppo1()],
        ['vivo', new privacyChannelVivo()],
        ['vivo1', new privacyChannelVivo1()],
        ['web', new privacyChannelWeb()],
        ['ks', new privacyChannelKS()],
        ['ks1', new privacyChannelKSinner()],
        ['xm', new privacyChannelXM()],
        ['mg', new pwrChannelMg()]


    ]);


    static getChannel(): privacyChannelWeb {
        return privacyChannel.channelMap.get(privacyChannel.channel);
    }


    static exit() {
        privacyChannel.getChannel().exit();
    }

    /** getSystemInfoSync */
    static getSysInfo(): any {
        return privacyChannel.getChannel().getSysInfo();
    }

    /* 是否有隐私政策 */
    static hasPrivacy(): boolean {
        return privacyChannel.getChannel().hasPrivacy();
    }
    static privacyReject(): number {
        return privacyChannel.getChannel().privacyReject();
    }
    /** 同意隐私政策 */
    static privacyAgree(): number {
        return privacyChannel.getChannel().privacyAgree();
    }
    static privacyNextPage(pageType: number): string {
        return privacyChannel.getChannel().privacyNextPage(pageType);
    }
    static privacyPrePage(pageType: number): string {
        return privacyChannel.getChannel().privacyPrePage(pageType);
    }
    static privacyPage1(pageType: number): string {
        return privacyChannel.getChannel().privacyPage1(pageType);
    }


    static init() {
        console.error("channel init");
        //注意web渠道不会调loginCall
        privacyChannel.getChannel().init();
    }

    public static toast(txt: string, startY: number = 0) {
        const bundle = cc.assetManager.getBundle('privacy');
        bundle.load('bh_toast', cc.Prefab, (err, prefab: cc.Prefab) => {
            if (err) {
                console.error(err);
                return;
            }
            const newNode: cc.Node = cc.instantiate(prefab);
            newNode.getComponent(bh_toast).show(txt, cc.director.getScene().getChildByName('Canvas'), startY);
        });
    }

    static showPrivacy(isToMain: boolean) {
        cc.assetManager.loadBundle('privacy', (err: any, bundle: cc.AssetManager.Bundle) => {
            if (err) {
                console.error(err);
                return;
            }
            bundle.load('privacyWin', cc.Prefab, (err, data: any) => {
                if (err) {
                    console.error(err);
                    return;
                }
                const node: cc.Node = cc.instantiate(data);
                node.parent = cc.director.getScene().getChildByName('Canvas');
                node.getComponent(privacyWin).init(isToMain);
            });
        });
    }

    static addVideo(parent: cc.Node, scale: number = 1, x: number = 0, y: number = 0) {
        cc.assetManager.loadBundle('privacy', (err: any, bundle: cc.AssetManager.Bundle) => {
            if (err) {
                console.error(err);
                return;
            }
            bundle.load('img/video', cc.SpriteFrame, (err, data: cc.SpriteFrame) => {
                if (err) {
                    console.error(err);
                    return;
                }
                const node: cc.Node = new cc.Node();
                node.addComponent(cc.Sprite).spriteFrame = data;
                node.parent = parent;
                node.scale = scale;
                node.setPosition(x, y);
            });
        });
    }

    static jpost(url: string, data: any, callback?: any) {
        if (!callback) {
            callback = function (err) {
                if (err) {
                    return console.error(err);
                }
            };
        }
        const xhr = new XMLHttpRequest();
        // const now = new Date().getTime();
        xhr.open('POST', url, true);
        xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
        const postData = JSON.stringify(data);
        // geckc.log('post url:' + url);
        // geckc.log('post j:' + postData);
        xhr.onload = function () {
            if (xhr.status == 200) {
                let j = { 'postERR': '' + xhr.responseText };
                try {
                    j = JSON.parse(xhr.responseText);
                } catch (e) {
                    console.error(e);
                    return callback(null, { 'code': '1111', 'err': 'jsonErr:' + xhr.responseText });
                }
                return callback(null, j);
            } else {
                return callback(null, { 'code': '' + xhr.status });
            }
        };
        xhr.onerror = function (err) {
            callback(err);
        };
        try {
            xhr.send(postData);
        } catch (e) {
            console.error(e);
        }
    };

}
